package com.cms.entity;

import com.cms.CommonAttribute;
import com.cms.enums.VIPEnums;
import com.cms.util.LockInfo;
import org.apache.commons.lang.StringUtils;

import com.cms.entity.base.BaseMember;
import com.cms.util.DBUtils;
import com.jfinal.plugin.activerecord.Page;

/**
 * Entity -会员
 *
 *
 *
 */
@SuppressWarnings("serial")
public class Member extends BaseMember<Member> {

    /**
     * 会员session名称
     */
    public static final String SESSION_MEMBER="session_member";

   /**
     * 查找会员分页
     *
     * @param pageable
     *            分页信息
     * @return 会员分页
     */
    public Page<Member> findPage(String mobile,Integer pageNumber,Integer pageSize){
        String filterSql = "";
        if(StringUtils.isNotBlank(mobile)){
            filterSql+= " and mobile like '%"+mobile+"%'";
        }
        String orderBySql = DBUtils.getOrderBySql("createDate desc");
        return paginate(pageNumber, pageSize, "select *", "from kf_member where 1=1 "+filterSql+orderBySql);
    }

    /**
     * 查找会员分页
     *
     * @param pageable
     *            分页信息
     * @return 会员分页
     */
    public Page<Member> findUserPage(String userId,Integer pageNumber,Integer pageSize){
        String filterSql = "";
        if(StringUtils.isNotBlank(userId)){
            filterSql+= " and parentId = '"+userId+"'";
        }
        String orderBySql = DBUtils.getOrderBySql("createDate desc");
        return paginate(pageNumber, pageSize, "select *", "from kf_member where 1=1 "+filterSql+orderBySql);
    }

    /**
     * 根据用户名查找会员
     *
     * @param username
     *          用户名
     * @return  会员
     */
    public Member findByUsername(String username){
        return findFirst("select * from kf_member where mobile=? ", username);
    }

    public Member findByOpenId(String openId){
        return findFirst("select * from kf_member where weixinOpenId=? ", openId);
    }

    public String getVipInfo(){
        return VIPEnums.getIdType(this.getVipLevel()).getName();
    }

    /**
     * 根据邀请码查找会员
     *
     * @param inviteCode
     *          邀请码
     * @return  会员
     */
    public Member findByInviteCode(String inviteCode){
        return findFirst("select * from kf_member where inviteCode=? ", inviteCode);
    }

    public Member updateMember(Order order,Long memberId){
        LockInfo lockInfo = new LockInfo(CommonAttribute.USER_LOCK + memberId);

        Member member=new Member().dao().findById(memberId);
        try {
            //更新用户信息
            member.setTjCount(member.getTjCount()+order.getQuantity());
            member.setAchievement(member.getAchievement().add(order.getAmount()));
            //更新VIP
            if(member.getVipLevel()<6){
                int newVip= VIPEnums.checkVip(CommonAttribute.BUY,member.getTjCount()).getKey();
                member.setVipLevel(newVip);
            }
            member.update();
        } catch (Throwable throwable) {
            lockInfo.unlock();
            throwable.printStackTrace();
        }
        return member;
    }
}
